package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.AdminService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admins")
@Slf4j
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 返回登录页面
     */
    @GetMapping("/adminLogin")
    public String showLoginPage(Model model) {
        model.addAttribute("admin", new Admin());
        return "adminLogin"; // 返回login.html视图
    }
    @GetMapping("/addDepartment")
    public String showAddDepartment() {
        return "addDepartment";
    }
    @GetMapping("/departmentList")
    public String showDepartmentList() {
        return "departmentList";
    }
    @GetMapping("/updateDepartment")
    public String showUpdateDepartment(@RequestParam Long id, Model model) {
        if (id != null) {
            model.addAttribute("departmentId", id);
        }
        return "updateDepartment";
    }
    @GetMapping("/doctorList")
    public String showdoctorList() { return "doctorList"; }
    @GetMapping("/addDoctor")
    public String showAddDoctor() {
        return "addDoctor";
    }
    @GetMapping("/updateDoctor")
    public String showUpdateDoctor(@RequestParam Long id, Model model) {
        if (id != null) {
            model.addAttribute("doctorId", id);
        }
        return "updateDoctor";
    }
    @GetMapping("/deleteDoctor")
    public String showDeleteDoctor() {
        return "deleteDoctor";
    }
    @GetMapping("/scheduleList")
    public String showScheduleList() {
        return "scheduleList";
    }
    @GetMapping("/addSchedule")
    public String showAddSchedule() {
        return "addSchedule";
    }
    @GetMapping("/updateSchedule")
    public String showUpdateSchedule() {
        return "updateSchedule";
    }
    @GetMapping("/waitingList")
    public String showWaitingList(){
        return "waitingList";
    }

    /**
     * 处理登录请求
     * @param  admin
     * @return
     */
    @PostMapping("/adminLogin")
    @ResponseBody
    public ResponseEntity<?> login(@RequestBody Admin admin, HttpSession session) {
        Admin authenticatedAdmin = adminService.authenticate(admin.getUsername(), admin.getPassword());

        if (authenticatedAdmin != null) {
            session.setAttribute("admin", authenticatedAdmin);
            System.out.println("Session ID: " + session.getId()); // 打印 Session ID
            System.out.println("Admin in Session: " + authenticatedAdmin); // 打印用户信息
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Login failed");
        }
    }

    /**
     * 获取科室信息
     * @return
     */
    @GetMapping("/getAllDepartment")
    @ResponseBody
    public List<Department> getAllDepartment(){
        return adminService.getAllDepartment();
    }

    /**
     * 添加科室信息
     * @param department
     */
    @PostMapping("/addDepartment")
    @ResponseBody
    public void addDepartment(@RequestBody Department department){
        adminService.addDepartment(department);
    }

    /**
     * 更新科室信息
     * @param department
     */
    @PutMapping("/updateDepartment")
    @ResponseBody
    public void updateDepartment(@RequestBody Department department){
        adminService.updateDepartment(department);
    }

    /**
     * 获取所有医生信息
     * @return
     */
    @GetMapping("/getAllDoctor")
    @ResponseBody
    public List<Doctor> getAllDoctor(){
        return adminService.getAllDoctor();
    }

    /**
     * 添加医生信息
     * @param doctor
     */
    @PostMapping("/addDoctor")
    @ResponseBody
    public void addDoctor(@RequestBody Doctor doctor){
        adminService.addDoctor(doctor);
    }

    /**
     * 修改医生信息
     * @param doctor
     */
    @PutMapping("/updateDoctor")
    @ResponseBody
    public void updateDoctor(@RequestBody Doctor doctor){
        adminService.updateDoctor(doctor);
    }

    /**
     * 删除医生信息
     * @param id
     */
    @DeleteMapping("/deleteDoctor/{id}")
    @ResponseBody
    public void deleteDoctor(@PathVariable Long id){
        adminService.deleteDoctor(id);
    }

    /**
     * 获取所有排班信息
     * @return
     */
    @GetMapping("/getAllScheduleInfoOfDoctor")
    @ResponseBody
    public List<ScheduleInfo> getAllScheduleInfoOfDoctor() {
        return adminService.getAllScheduleInfoOfDoctor();
    }

    /**
     * 修改排班信息
     * @param scheduleInfo
     */
    @PutMapping("/updateSchdule")
    @ResponseBody
    public void updateSchdule(@RequestBody ScheduleInfo scheduleInfo){
        log.info("scheduleInfo: " + scheduleInfo);
        adminService.updateSchdule(scheduleInfo);
    }
    /**
     * 添加排班信息
     * @param schedule
     */
    @PostMapping("/addSchedule")
    @ResponseBody
    public void addSchedule(@RequestBody Schedule schedule){
        adminService.addSchedule(schedule);
    }

    /**
     * 删除排班信息
     * @param id
     */
    @DeleteMapping("/deleteSchedule/{id}")
    @ResponseBody
    public void deleteSchedule(@PathVariable Long id){
        adminService.deleteSchedule(id);
    }

    /**
     * 根据id获取排班信息
     * @param id
     * @return
     */
    @GetMapping("/getScheduleById/{id}")
    @ResponseBody
    public Schedule getScheduleById(@PathVariable Long id) {
        return adminService.getScheduleById(id);
    }

    /**
     * 动态模糊搜索排班信息
     * @param scheduleInfo
     * @return
     */
    @PostMapping("/searchScheduleInfo")
    @ResponseBody
    public List<ScheduleInfo> searchScheduleInfo(@RequestBody ScheduleInfo scheduleInfo) {
        log.info("scheduleInfo: " + scheduleInfo);
        List<ScheduleInfo> scheduleInfoS = adminService.searchScheduleInfo(scheduleInfo);
        return scheduleInfoS;
    }

    /**
     * 获取所有等待列表
     * @return
     */
    @GetMapping("/getAllWaitingList")
    @ResponseBody
    public List<WaitingDetail> getALlWaitingList(){
        return adminService.getALlWaitingList();
    }

    /**
     * 修改等待列表信息
     * @param waiting
     */
    @PutMapping("/updateWaiting")
    @ResponseBody
    public void updateWaiting(@RequestBody Waiting waiting){
        adminService.updateWaiting(waiting);
    }

    /**
     * 取消等待
     * @param id
     */
    @PutMapping("/cancelWaiting/{id}")
    @ResponseBody
    public void cancelWaiting(@PathVariable Long id){
        adminService.cancelWaiting(id);
    }

    /**
     * 设置叫号时间
     * @param id
     */
    @PostMapping("/setCallTime")
    @ResponseBody
    public void setCallTime(@RequestParam Long id) {
        adminService.setCallTime(id);
    }

    /**
     * 接诊患者
     * @param id
     */
    @PostMapping("/consultWaiting/{id}")
    @ResponseBody
    public void consultWaiting(@PathVariable Long  id){
        adminService.consultWaiting(id);
    }

    /**
     * 结束等待
     * @param id
     */
    @PutMapping("/finishWaiting/{id}")
    @ResponseBody
    public void finishWaiting(@PathVariable Long id){
        adminService.finishWaiting(id);
    }
}
